package cn.itcast.bos.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.MenuRepository;
import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
@Service
@Transactional
public class MenuServiceimpl implements MenuService{
    @Autowired
    private MenuRepository menuRepository;
	@Override
	public List<Menu> findAll() {
		// TODO Auto-generated method stub
		return menuRepository.findAll();
	}
	@Override
	public void save(Menu menu) {
		//防止父菜单提交无效的值
	     if(menu.getParentMenu() !=null    && menu.getParentMenu().getId() ==0){
	    	 menu.setParentMenu(null);
	     }
	     menuRepository.save(menu);
	}
	@Override
	public List<Menu> findByUser(User user) {
		if(user.getUsername().equals("admin")){
			//如果是admin就可以使用所有的菜单
			return menuRepository.findAll();
		}else{
			System.out.println("关联的菜单"+menuRepository.findByUser(user.getId()));
			System.out.println("用户id"+user.getId());
			//通过用户id查找关联的菜单
			return menuRepository.findByUser(user.getId());
		}
	 
	}

}
